﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Media;
using System.ComponentModel;

namespace AllenHung.FeedReader
{
    public partial class FeedPickerPage : PhoneApplicationPage
    {
        private Dictionary<int, FeedSource> selected = null;
        public FeedPickerPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            ContentPanel.ItemsSource = MainPage.AllFeedSources;
            selected = new Dictionary<int, FeedSource>();
        }

        private void OK_Click(object sender, EventArgs e)
        {
            if (selected.Count > 0)
            {
                foreach (FeedSource fs in selected.Values)
                {
                    MainPage.SubscribedFeedSources.Add(fs);
                }
                //BackgroundWorker databaseWorker = new BackgroundWorker();
                //databaseWorker.DoWork += new DoWorkEventHandler(backroungWorker_UpdateDatabase);
                //databaseWorker.RunWorkerAsync();
                foreach (int id in selected.Keys)
                {
                    FeedSource fs = MainPage.MainModel.FeedSources.Single<FeedSource>(f => f.SourceId == id);
                    fs.IsSubscribed = true;
                }
                MainPage.MainModel.SubmitChanges();
            }
            BackToMainPage();
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            BackToMainPage();
        }

        private void BackToMainPage()
        {
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        private void AddCustomFeedClick(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/AddCustomFeedPage.xaml", UriKind.Relative));
        }
        private void FeedList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
        private void SrcButtonClick(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;
            FeedSource src = b.DataContext as FeedSource;
            if (selected.ContainsKey(src.SourceId))
            {
                b.Background = null;
                selected.Remove(src.SourceId);
            }
            else
            {
                b.Background =  App.Current.Resources["FeedBrush"] as SolidColorBrush;
                selected.Add(src.SourceId, src);
            }
        }

        private void backroungWorker_UpdateDatabase(object sender, DoWorkEventArgs e)
        {
            foreach (int id in selected.Keys)
            {
                FeedSource fs = MainPage.MainModel.FeedSources.Single<FeedSource>(f => f.SourceId == id);
                fs.IsSubscribed = true;
            }
            MainPage.MainModel.SubmitChanges();
        }
    }
}